package com.yiboshi.rehrs.feign;

import com.alibaba.fastjson.JSONObject;
import feign.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Objects;

/**
 * @author jack 2025/3/24
 */
@Slf4j
public class FeignResultDecoder  implements Decoder {
    @Override
    public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
        String body = Util.toString(response.body().asReader());
        JSONObject jsonObject = JSONObject.parseObject(body);
        if(jsonObject!=null){
            String code = jsonObject.getString("resCode");
            if(!Objects.equals(code,"A00001")){
                log.error("res:{}",jsonObject);
            }
        }
        /*if(jsonObject!=null){
            String msg = jsonObject.getString("msg");
            if(msg.contains("重新登录")){
                throw new DecodeException(msg);
            }
        }*/
        return jsonObject;
    }
}
